#!/usr/bin/perl

# Early in your program:

use LWP 5.64; # Loads all important LWP classes, and makes
				#  sure your version is reasonably recent.
				
use DBI;
$startrun = time();
@ns_headers = 
(
	'User-Agent' => 'Mozilla/4.76 [en] (Win98; U)',
	'Accept-Charset' => 'iso-8859-1,*,utf-8',
	'Accept-Language' => 'en-US',
);
$browser = LWP::UserAgent->new;
####################################################################################################
####################################################################################################
####################################################################################################
sub trim($)
{
	my $string = shift;
	$string =~ s/^\s+//;
	$string =~ s/\s+$//;
	return $string;
}
sub now()
{
	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
	my $datestring = sprintf("%4d-%02d-%02d %02d:%02d:%02d",($year + 1900),($mon+1),$mday,$hour,$min,$sec);
	return $datestring
}
sub parseList
{
	$baseURL = $_[0];
	$id = $_[1];
	
	$countLetter->execute();
	$countLetter->bind_columns(\$initialCount);
	$countLetter->fetch();
	$url = "$baseURL/$id";
	my $response = $browser->get( $url, @ns_headers );
	unless ($response->is_success) 
	{
		warn "Unable to load page for '$id'\n";
		next;
	}
	$string = $response->decoded_content;
	while ($string =~ m/<a href="http:\/\/watch-movies-az.com\/movie\/([^"\n]+)[^>]+>\s*\n*\s*(.*)\s+\((\d*)\)\s*<\/a>/g) 
	{
		$code = $1;
		$name = trim($2);
		$year = $3;
		if( substr($name, 0, 1) =~ /[a-zA-Z]/ )
		{
			$char = uc(substr($name, 0, 1));
		}
		else
		{
			$char = '#';
		}
		$insert->execute($name,$year,$char,$code);
	}
	$countLetter->execute();
	$countLetter->bind_columns(\$finalCount);
	$countLetter->fetch();
	$count = $finalCount - $initialCount;
	if( $count > 0 )
	{
		print "$id: $count rows added\n";
	}
	return $count;
}
####################################################################################################
####################################################################################################
####################################################################################################
print "~~~ MOVIEPARSE.PL START TIME: ".now()." ~~~\n";
my $db = DBI -> connect("dbi:Pg:dbname=watchmovies;host=localhost;port=4232","watchMoviesUser","Movies42FTW") || die $DBI::errstr;
$getAll = $db->prepare('SELECT movie_wmaz_code FROM TMovies WHERE movie_wmaz_parsed = FALSE');
$parse = $db->prepare('SELECT F_TMovies_Parse_Wmaz(?,?,?);');

$getAll->execute();
$getAll->bind_columns(\$code);
$baseURL = "http://watch-movies-az.com/movie/";
$totalTime = time() - $startrun;
$totalCount = 0;
#while($totalTime < (45*60) && $getAll->fetch() ) # Roule 45 minutes au maximum
while($totalTime < (1 * 10) && $getAll->fetch() ) # Roule 1 minute au maximum
{
	my $url = "$baseURL$code";
	my $response = $browser->get( $url, @ns_headers );
	unless ($response->is_success) 
	{
		warn "Unable to load '$url'\n";
		next;
	}
	my $string = $response->decoded_content;
	$string =~ m/<a href='http:\/\/www.imdb.com\/title\/([^\/]+)(.*\n*)*<span class="no-summary-vid">\s([^<]+)/i;
	my $imdb = $1;
	my $desc = $3;
	$parse->execute($code,$desc,$imdb);
	print "\n### $code parsed ! imdb: $imdb\n$desc\n";
	$totalCount = $totalCount + 1;
	$totalTime = time() - $startrun;
}
print "\n> TOTAL parsed: $totalCount\n";
$getAll->finish();
$parse->finish();
$db->disconnect();
print "~~~ MOVIELIST.PL END TIME: ".now()." ~~~\n";
